package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    @Reference
    private UserService userService;



    @RequestMapping("/findByMenuIds")
    public Result  findMenuIds(Integer id){
        try {
            Integer[] menuIds = menuService.findMenuIds(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }

    }





    /**
     * 查询所有菜单
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> all = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,all);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }









    /**
     *分页查询
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {

            Integer pageSize = queryPageBean.getPageSize();
            Integer currentPage = queryPageBean.getCurrentPage();

            PageResult page = menuService.findPage(currentPage, pageSize, queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 异步获取所有菜单
     */
    @RequestMapping("/findParent")
    public  Result findParent(){
        try {
            List<Menu> parents = menuService.findParent();
            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,parents);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 新增菜单
     */

    @RequestMapping("/add")
    public Result addMenu(@RequestBody Menu menu){

        try {
            menuService.addMenu(menu);
            return  new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 查询菜单数据
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return  new Result(true,MessageConstant.ADD_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 编辑表单数据
     */
    @RequestMapping("/update")
    public Result updateMenu(@RequestBody Menu menu){

        try {
            menuService.updateMenu(menu);
            return  new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 删除表单数据
     */
    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        try {
            menuService.delete(id);
            return  new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 查看树状图
     */
    @RequestMapping("/findChildren")
    public Result findChildren(Integer id){
        try {
            List<Menu> childrens = menuService.findChildrens(id);

            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,childrens);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 主页菜单显示
     */

    @RequestMapping("/findMenus")
    public Result findMenus(){
        try {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            String username = userDetails.getUsername();
            List<Menu> menus = userService.findByUsername01(username);

     /*      List<Menu> menus = menuService.findMenus();

            System.out.println(menus.toString());*/
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
